package model

import (
	"go-devops/dao"
)

type TaskNamespace struct {
	Id   string `json:"id" gorm:"primaryKey"`
	Name string `json:"name"`
}

func (TaskNamespace) TableName() string {
	return "task_namespace"
}

func GetTaskNamespaceList() (list []*TaskNamespace, err error) {
	if err := dao.DB.Find(&list).Error; err != nil {
		return nil, err
	}
	return list, err
}

func GetTaskNamespaceCount(name string) (count int64, err error) {
	err = dao.DB.Model(&TaskNamespace{}).Where("name = ?", name).Count(&count).Error
	return count, err
}

func AddTaskNamespace(taskNamespace *TaskNamespace) (err error) {
	if err := dao.DB.Create(taskNamespace).Error; err != nil {
		return err
	}
	return nil
}

func DeleteTaskNamespace(id string) (err error) {
	if err := dao.DB.Where("id = ?", id).Delete(&TaskNamespace{}).Error; err != nil {
		return err
	}
	return nil
}

func QueryTaskNamespaceById(id string) (taskNamespace *TaskNamespace, err error) {
	if err := dao.DB.Where("id = ?", id).First(&taskNamespace).Error; err != nil {
		return nil, err
	}
	return taskNamespace, nil
}
